import random
class GeoBox(object):
	def __init__(self):
		self.index = {}
		self.cellsize = 100;
		
	def clear(self):
		self.index = {}

	def get_cell(self,x,y):
		return x/self.cellsize,y/self.cellsize
	
	def add_point(self,x,y,item):
		key = self.get_cell(x,y)
		self.index.setdefault(str(key), []).append(item)
		
	def get_near_by(self,cell,cell_range=1,):
		near_by = []
		c = []
		for i in xrange(-cell_range,cell_range+1):
			c.append(i)
			
		for i in c:
			for j in c:
				kx = cell[0] - i
				ky = cell[1] - j
				key = str((kx,ky))
				if self.index.has_key(key):
					near_by.extend(self.index[key])
					
		return near_by
		
	def __str__(self):
		return str('\n'.join(['%s,%s' % (k,v) for k,v in self.index.items()]))
		
		
if __name__ == "__main__":
	gb = geobox()
	for i in range(200):
		x = random.randint(0,1000)
		y = random.randint(0,1000)	
		gb.add_point(x,y,i)
	print str(gb)
	print '-' * 40
	print gb.get_near_by(300,300,1)